package com.clx.chat.service.Impl;

import com.clx.chat.controller.RabbitmqSender;
import com.clx.chat.mapper.ContactMapper;
import com.clx.chat.mapper.MessageMapper;
import com.clx.chat.mapper.UserMapper;
import com.clx.chat.service.ContactService;

import com.clx.common.domain.entity.Contact;
import com.clx.common.domain.entity.DiskUser;
import com.clx.common.utils.DateUtils;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 联系人表(Contact)表服务实现类
 *
 * @author clx
 * @since 2025-02-22 17:33:49
 */
@Service("contactService")
@RequiredArgsConstructor
public class ContactServiceImpl implements ContactService {

    private final ContactMapper contactMapper;

    private final MessageMapper messageMapper;

    private final UserMapper userMapper;

    private final RabbitmqSender rabbitmqSender;
    /**
     * 查询数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public Contact selectContactById(Long id) {
        return contactMapper.selectContactById(id);
    }

    /**
     * 查询列表
     *
     * @param contact 筛选条件
     * @return 查询结果
     */
    @Override
    public List<Contact> selectContactList(Contact contact) {
        Contact entity = contact;
        return (contactMapper.selectContactList(entity));
    }

    /**
     * 新增数据
     *
     * @param contact 实例对象
     * @return 受影响的行数，新增成功通常返回1，失败返回0等
     */
    @Override
    @Transactional
    public int insertContact(Contact contact) {
        DiskUser userTemp=new DiskUser();
        userTemp.setId(Long.valueOf(contact.getUserOther()));
        DiskUser userOther = userMapper.selectByUser(userTemp);
        Contact entity = contact;
        entity.setCreateTime(DateUtils.getNowDate());
        entity.setAvatar(userOther.getAvatar());
        int i = 0;
        try {
            i = contactMapper.insertContact(entity);
            //同时给另一个用户添加联系人
            //根据userSelf查询用户
            DiskUser user=new DiskUser();
            user.setId(Long.valueOf(contact.getUserSelf()));
            DiskUser userSelf = userMapper.selectByUser(user);
            Contact contactOther = Contact.builder()
                    .contactName(userSelf.getUsername())
                    .createTime(entity.getCreateTime())
                    .userOther(entity.getUserSelf())
                    .userSelf(entity.getUserOther())
                    .avatar(userSelf.getAvatar())
                    .status(0)
                    .lastMessage(entity.getLastMessage())
                    .build();
            //创造另一个用户记录
            contactMapper.insertContact(contactOther);
            //发送到rabbitmq 通知被加好友的用户更新contact
            rabbitmqSender.addContact(contactOther.getUserSelf());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return i;
    }

    /**
     * 修改数据
     *
     * @param contact 实例对象
     * @return 受影响的行数，一般成功修改返回1
     */
    @Override
    public int updateContact(Contact contact) {
        Contact entity = contact;
        entity.setUpdateTime(DateUtils.getNowDate());
        return (contactMapper.updateContact(entity));
    }

    /**
     * 批量删除数据
     *
     * @param ids 需要删除的主键数组
     * @return 受影响的行数，即成功删除的记录数
     */
    @Override
    public int deleteContactByIds(Long[] ids) {
        return (contactMapper.deleteContactByIds(ids));
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功（以受影响行数判断，成功返回1等）
     */
    @Override
    public int deleteContactById(Long id) {
        return (contactMapper.deleteContactById(id));
    }


    /**
     * 判断是否添加过联系人 0添加过  1没添加
     */
    @Override
    public int isAdd(Contact contact) {
        Contact finalContact= contactMapper.isAdd(contact);
        if (ObjectUtils.isNotEmpty(finalContact)){
            //说明添加过
            return 0;
        }
        return 1;
    }
}
